// JavaScript Document

/////////////////////////////////////////////////////////////////////////////
//PJS
var PJS = new Object();
PJS.VERSION = "1.0.0.1";
PJS.AUTHOR = "jianyu";
PJS.COPYRIGHT = "PPEMI.COM";
PJS.BASE_URL = BASE_URL;
PJS.USER_AGENT = window.navigator.userAgent.toLowerCase();
PJS.IS_OPERA = (PJS.USER_AGENT.indexOf("opera")!=-1); 
PJS.IS_IE = ((PJS.USER_AGENT.indexOf("msie")!=-1)&&((document.all&&!PJS.IS_OPERA))); 
PJS.IS_MOZILLA = (PJS.USER_AGENT.indexOf("gecko")!=-1); //is that right?
PJS.IS_NS = (navigator.appName == "Netscape") ? 1 : 0;
/////////////////////////////////////////////////////////////////////////////
//METHODS
PJS.$E = function(el){
	if(document.getElementById){
		return document.getElementById(el);
	} else if(document.layers){
		document.layers[el];
	} else {
		return eval("document.all."+el);
	}
};
PJS.goto = function(url){
	document.location.href = PJS.BASE_URL+url;
};
PJS.redirect = function(url){
	document.location.href = url;
};
PJS.refresh = function(){
	document.location.href = document.location.href;
};
/////////////////////////////////////////////////////////////////////////////
//PJS.list
PJS.list = function(){};
PJS.list.show =  function(type,length){
	if(!length){
		length = 10;
	}
	if($E("list_"+type+"_h")){
		$E("list_"+type+"_h").style.display = "";
	}
	for(var i=0; i<length; i++){
		if($E("list_"+type+"_"+i)){
			$E("list_"+type+"_"+i).style.display = "";
		}
	}
};
PJS.list.hide =  function(type,length){
	if(!length){
		length = 10;
	}
	if($E("list_"+type+"_h")){
		$E("list_"+type+"_h").style.display = "none";
	}
	for(var i=0; i<length; i++){
		if($E("list_"+type+"_"+i)){
			$E("list_"+type+"_"+i).style.display = "none";
		}
	}
};
PJS.list.toggle =  function(type,length){
	if(!length){
		length = 30;
	}
	if($E("list_"+type+"_h")){
		if($E("list_"+type+"_h").style.display == ""){
			$E("list_"+type+"_h").style.display = "none";
		} else {
			$E("list_"+type+"_h").style.display = "";
		}
	}
	for(var i=0; i<length; i++){
		if($E("list_"+type+"_"+i)){
			if($E("list_"+type+"_"+i).style.display == ""){
				$E("list_"+type+"_"+i).style.display = "none";
			} else {
				$E("list_"+type+"_"+i).style.display = "";
			}
		}
	}
};
PJS.list.gc = function(obj){
	while(obj.nodeName != "TABLE"){
		obj = obj.parentNode;
	}
	return obj;
};
PJS.list.selectAll = function(obj,chk){
	if(chk == null){
		chk = "cb";
	}
	var c = null;
	if(obj.form){
		c = obj.form;
	} else if(obj.parentNode.parentNode.nodeType){
		c = PJS.list.gc(obj);
	}
	var a = c.getElementsByTagName("INPUT");
	for(var i=0; i<a.length; i++){
		if(a[i].name == chk || a[i].name == chk+"[]"){
			a[i].checked = obj.checked;
		}
	}
};
PJS.list.hasSelected = function(obj,chk){
	if(chk == null){
		chk = "cb";
	}
	var c = null;
	if(obj.form){
		c = obj.form;
	} else if(obj.parentNode.parentNode.nodeType){
		c = PJS.list.gc(obj);
	}
	var a = c.getElementsByTagName("INPUT");
	for(var i=0; i<a.length; i++){
		if(a[i].name == chk || a[i].name == chk+"[]"){
			if(a[i].checked){
				return true;
			}
		}
	}
	return false;
};
PJS.list.getSelected = function(obj,chk){
	if(chk == null){
		chk = "cb";
	}
	var ss = [];
	var c = null;
	if(obj.form){
		c = obj.form;
	} else if(obj.parentNode.parentNode.nodeType){
		c = PJS.list.gc(obj);
	}
	var a = c.getElementsByTagName("INPUT");
	for(var i=0; i<a.length; i++){
		if(a[i].name == chk || a[i].name == chk+"[]"){
			if(a[i].checked){
				ss.push(a[i].value);
			}
		}
	}
	return ss;
};
PJS.list.getItems = function(obj,name){
	if(name == null){
		name = "cb";
	}
	var ss = [];
	var c = null;
	if(obj.form){
		c = obj.form;
	} else if(obj.parentNode.parentNode.nodeType){
		c = PJS.list.gc(obj);
	}
	var a = c.getElementsByTagName("INPUT");
	for(var i=0; i<a.length; i++){
		if(a[i].name == name || a[i].name == name+"[]"){
			ss.push(a[i]);
		}
	}
	return ss;
};
PJS.list.getValues = function(obj,name){
	if(name == null){
		name = "cb";
	}
	var ss = [];
	var c = null;
	if(obj.form){
		c = obj.form;
	} else if(obj.parentNode.parentNode.nodeType){
		c = PJS.list.gc(obj);
	}
	var a = c.getElementsByTagName("INPUT");
	for(var i=0; i<a.length; i++){
		if(a[i].name == name || a[i].name == name+"[]"){
			ss.push(a[i].value);
		}
	}
	return ss;
};
PJS.list.getValuesTotal = function(obj,name){
	var total = 0;
	var ss = PJS.list.getValues(obj,name);
	for(var i=0; i<ss.length; i++){
		total += parseFloat(ss[i]);
	}
	return total;
};
PJS.list.doActionCheck = function(action,obj,handler,chk){
	if(chk == null || chk == ""){
		chk = "cb";
	}
	var _action = "";
	if(obj.form.action){
		_action = obj.form.action.value;
	} else {
		//var t = document.createElement("INPUT");
		//t.setAttribute("type","hidden");
		//t.setAttribute("name","action");
		//obj.form.appendChild(t);
		var t = PJS.createInput("hidden","action");
		if(!t ) return;
		obj.form.appendChild(t);
	}
	obj.form.action.value = action;
	var a = obj.form.getElementsByTagName("INPUT");
	var ab = 0;
	var ac = false;
	var ad = [];
	for(var i=0; i<a.length; i++){
		if(a[i].name == chk || a[i].name == chk+"[]"){
			if(a[i].checked){
				ac = true;
				ad.push(a[i].value);
			}
			ab++;
		}
	}
	if(ab > 0 && ac == false){
		obj.form.action.value = _action;
		if(obj.tagName.toUpperCase()=="SELECT"){
			if(obj.options.length > 0) {
				if(obj.options[0].value == "" || obj.options[0].value == "0"){
					obj.options[0].selected = true;
				} else {
					obj.options[0].selected = true;
				}
			} else {
				obj.value = "";
			}
		}
		alert("Please select the items");
		return false;
	}
	if(confirm("Are you sure you want to do the operation?") == true){
		if(handler && typeof(handler) == "function"){
			handler(obj.form,ad);
		} else if(handler && typeof(handler) == "string"){
			handler(obj.form,ad);
		} else if(handler && handler.length > 1 && typeof(handler[0]) == "function" && typeof(handler[1]) == "object"){
			handler[0].call(handler[1],obj.form,ad);
		} else {
			obj.form.submit();
		}
		return true;
	} else {
		obj.form.action.value = _action;
		if(obj.tagName.toUpperCase()=="SELECT"){
			if(obj.options.length > 0) {
				if(obj.options[0].value == "" || obj.options[0].value == "0"){
					obj.options[0].selected = true;
				} else {
					obj.options[0].selected = true;
				}
			} else {
				obj.value = "";
			}
		}
	}
	return false;
};
PJS.list.doActionField = function(action,obj){
	if(obj.value==""){
		alert("select operation");
		return false;
	}
	PJS.list.doActionCheck(action,obj);
};
PJS.list.doActionFieldA = function(action,obj,value){
	obj.value = value;
	PJS.list.doActionCheck(action,obj);
};
PJS.list.doAction = function(action,obj,post){
	if(obj.form){
		if(obj.form.action){
			//
		} else {
			//var t = document.createElement("INPUT");
			//t.setAttribute("type","hidden");
			//t.setAttribute("name","action");
			//obj.form.appendChild(t);
			var t = PJS.createInput("hidden","action");
			if(!t ) return;
			obj.form.appendChild(t);
		}
		if(post){
			obj.form.setAttribute("action",post);
		}
		obj.form.action.value = action;
		obj.form.submit();
	}
};
PJS.list.doDelete = function(action,id,msg){
	var form = PJS.getParent(obj,"FORM");
	var chk = false;
	var row = null;
	var cb = form.elements["cb[]"];
	if(isUndefined(cb.length)){
		if(cb.value == id){
			chk = true;
			row = PJS.getParent(cb,"TR");
		}
	} else {
		for(var i=0; i<cb.length; i++){
			if(cb[i].value == id){
				chk = true;
				row = PJS.getParent(cb[i],"TR");
			}
		}
	}
	if(chk && row && PJS.list.delchk(msg)){
		var params = {"action": action,"id": id};
		xh_d("",params,function(r){
				if(r == "success"){
					PJS.removeE(row);
				} else {
					alert("Error");
				}
				});
	}
};
PJS.list.del = function(action,id,msg){
	if(!msg ) msg = "Delete?";
	if(confirm(msg)){
		return true;
	}
	
	return false;
};
PJS.list.delchk = function(msg){
	if(!msg ) msg = "Delete?";
	if(confirm(msg)){
		return true;
	}
	
	return false;
};
PJS.list.delchkgo = function(url,msg){
	if(!msg ) msg = "Delete?";
	if(confirm(msg)){
		PJS.redirect(url);
	}
	
	return false;
};
/////////////////////////////////////////////////////////////////////////////
//PJS.validator
PJS.validator = function(){};
PJS.validator.registerFormAction = function(){
	//common form validate
	if(arguments.length > 1){
		var forms = arguments;
	} else if(arguments.length == 1){
		var forms = [arguments[0]];
	} else {
		var forms = document.forms;
	}
	for(var i=0; i<forms.length; i++){
		if((forms[i].method == "post" || forms[i].method == "get") && (isUndefined(forms[i].onsubmit) || forms[i].onsubmit == null)){
			var fm = forms[i];
			fm.onsubmit = function(){
					var form = this;
					return PJS.validator.checkFormValid(form);
						};
			for(var j=0; j<fm.elements.length; j++){
				fm.elements[j].onblur = function(){
					var element = this;
					return PJS.validator.checkElementValid(element);
						};
			}
		}
	}
};
PJS.validator.checkElementValid = function(element){
	var form_element_msg = [];
	var vfs = [];
	var berr = false;
	var v = element.getAttribute("valid");
	var _v = element.getAttribute("_valid");
	if(v && (_v == null || _v == 1) && (isUndefined(element.diabled) || element.diabled == false)){
		var vs = v.split(",");
		for(var j in vs){
			v = vs[j];
			switch(v){
				case "number":
					if(element.value == ""){
					} else if(IsNumeric(element.value) == false){
						form_element_msg.push("The field must be number");
						PJS.addClass(element,"form-element-error");
						berr = true;
					}
					break;
				case "decimal":
				case "percent":
					if(element.value == ""){
						form_element_msg.push("The field must be decimal");
						PJS.addClass(element,"form-element-error");
						berr = true;
					} else if(IsDecimal(element.value) == false){
						form_element_msg.push("The field must be decimal");
						PJS.addClass(element,"form-element-error");
						berr = true;
					}
					break;
				case "float":
					if(element.value == ""){
					} else if(IsFloat(element.value) == false){
						form_element_msg.push("The field must be float");
						PJS.addClass(element,"form-element-error");
						berr = true;
					}
					break;
				case "price":
					if(element.value == ""){
					} else if(IsFloat(element.value) == false){
						form_element_msg.push("The field must be price");
						PJS.addClass(element,"form-element-error");
						berr = true;
					}
					break;
				case "time":
					break;
				case "email":
					if(element.value == ""){
						form_element_msg.push("The field must be email address");
						PJS.addClass(element,"form-element-error");
						berr = true;
					} else if(PJS.checkEmail(element.value) == false){
						form_element_msg.push("The field must be a valid email address");
						PJS.addClass(element,"form-element-error");
						berr = true;
					}
					break;
				case "require":
				case "notnull":
				case "not_null":
					if(element.type == "checkbox" && element.checked == false){
						var msg = element.getAttribute("valid_"+v+"_msg");
						if(!msg) msg = "The field is required";
						form_element_msg.push(msg);
						return false;
					} else if(element.value == "" || element.value == "0"){
						var msg = element.getAttribute("valid_"+v+"_msg");
						if(!msg) msg = "The field is required";
						form_element_msg.push(msg);
						PJS.addClass(element,"form-element-error");
						berr = true;
					}
					break;
			}
		}
		if(berr==false){
			PJS.removeClass(element,"form-element-error");
		}
	} else if(v){
		//
	} else if(element.getAttribute("require") == "true" || element.getAttribute("require") == "1"){
		if(element.tagName.toUpperCase() == "SELECT"){
			if(element.value == ""){
				form_element_msg.push("The field must be selected");
				PJS.addClass(element,"form-element-error");
				berr = true;
			} else {
				PJS.removeClass(element,"form-element-error");
			}
		} else if(element.tagName.toUpperCase() == "INPUT"){
			if(element.type.toUpperCase() == "CHECKBOX" || element.type.toUpperCase() == "RADIO"){
				var en = element.name;
				if(PJS.list.hasSelected(element,en) == false){
					form_element_msg.push("The field must be selected");
					PJS.addClass(element.parentNode,"form-element-error");
					berr = true;
				} else {
					PJS.removeClass(element.parentNode,"form-element-error");
				}
			} else {
				if(element.value == ""){
					form_element_msg.push("The field must be entered");
					PJS.addClass(element,"form-element-error");
					berr = true;
				} else {
					PJS.removeClass(element,"form-element-error");
				}
			}				
		} else {
			vfs.push(element.name);
		}
	}
	
	return berr;
};
PJS.validator.checkFormValid = function(form){
	var form_element_focus = null;
	var form_element_msg = [];
	var checkbefore = form.getAttribute("checkbefore");
	var checkafter = form.getAttribute("checkafter");
	if(checkbefore != ""){
		var _ca = eval(checkbefore);
		if(_ca == false){
			return false;
		}
	}
	if(form.field_name && form.field_name.value == ""){
		form_element_msg.push("Please enter the name");
		form.field_name.focus();
		return false;
	} else if(form.field_subject && form.field_subject.value == ""){
		form_element_msg.push("Please enter the subject");
		form.field_subject.focus();
		return false;
	}
	if(form.category && form.field_category){
		if(form.field_category.value == "" && form.category.value == ""){
			form_element_msg.push("Please enter the category or select one");
			form.category.focus();
			return false;
		}
	}
	if(form.group && form.field_group){
		if(form.field_group.value == "" && form.group.value == ""){
			form_element_msg.push("Please enter the group or select one");
			form.group.focus();
			return false;
		}
	}
	/*
	if(form.field_email_address){
		if(form.field_email_address.value == ""){
			form.field_email_address.focus();
			return false;
		} else if(PJS.checkEmail(form.field_email_address.value) == false){
			//form.field_email_address.select();
			form.field_email_address.focus();
			return false;
		}
	}
	*/
	if(form.field_start_time && form.field_end_time && form.field_end_time.value.length > 0){
		if(form.field_start_time.value >= form.field_end_time.value){
			form_element_msg.push("The start time field must be before the end time");
			//form.field_end_time.focus();
			return false;
		}
	} else if(form.date_from && form.date_to && form.date_to.value.length > 0){
		if(form.date_from.value > form.date_to.value){
			form_element_msg.push("The start time field must be before the end time");
			form.date_from.focus();
			return false;
		}
	}
	if(form.field_sort_order && IsNumeric(form.field_sort_order.value) == false){
		form_element_msg.push("The sort order field must be number");
		form.field_sort_order.focus();
		return false;
	}
	var vfs = [];
	var berr = false;
	for(var i=0; i<form.elements.length; i++){
		var v = form.elements[i].getAttribute("valid");
		var _v = form.elements[i].getAttribute("_valid");
		if(v && (_v == null || _v == 1) && (isUndefined(form.elements[i].diabled) || form.elements[i].diabled == false)){
			var vs = v.split(",");
			for(var j in vs){
				v = vs[j];
				switch(v){
					case "number":
						if(form.elements[i].value == ""){
						} else if(IsNumeric(form.elements[i].value) == false){
							form_element_msg.push("The field must be number");
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						}
						break;
					case "decimal":
					case "percent":
						if(form.elements[i].value == ""){
							form_element_msg.push("The field must be decimal");
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						} else if(IsDecimal(form.elements[i].value) == false){
							form_element_msg.push("The field must be decimal");
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						}
						break;
					case "float":
						if(form.elements[i].value == ""){
						} else if(IsFloat(form.elements[i].value) == false){
							form_element_msg.push("The field must be float");
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						}
						break;
					case "price":
						if(form.elements[i].value == ""){
						} else if(IsFloat(form.elements[i].value) == false){
							form_element_msg.push("The field must be price");
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						}
						break;
					case "time":
						break;
					case "email":
						if(form.elements[i].value == ""){
							form_element_msg.push("The field must be email address");
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						} else if(PJS.checkEmail(form.elements[i].value) == false){
							form_element_msg.push("The field must be a valid email address");
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						}
						break;
					case "require":
					case "notnull":
					case "not_null":
						if(form.elements[i].type == "checkbox" && form.elements[i].checked == false){
							var msg = form.elements[i].getAttribute("valid_"+v+"_msg");
							if(!msg) msg = "The field is required";
							form_element_msg.push(msg);
							return false;
						} else if(form.elements[i].value == "" || form.elements[i].value == "0"){
							var msg = form.elements[i].getAttribute("valid_"+v+"_msg");
							if(!msg) msg = "The field is required";
							form_element_msg.push(msg);
							if(form_element_focus == null ) form_element_focus = form.elements[i];
							PJS.addClass(form.elements[i],"form-element-error");
							berr = true;
						}
						break;
				}
			}
		} else if(v){
			//
		} else if(form.elements[i].getAttribute("require") == "true" || form.elements[i].getAttribute("require") == "1"){
			if(form.elements[i].tagName.toUpperCase() == "SELECT"){
				if(form.elements[i].value == ""){
					if(form_element_focus == null ) form_element_focus = form.elements[i];
					form_element_msg.push("The field must be selected");
					PJS.addClass(form.elements[i],"form-element-error");
					berr = true;
				} else {
					PJS.removeClass(form.elements[i],"form-element-error");
				}
			} else if(form.elements[i].tagName.toUpperCase() == "INPUT"){
				if(form.elements[i].type.toUpperCase() == "CHECKBOX" || form.elements[i].type.toUpperCase() == "RADIO"){
					var en = form.elements[i].name;
					if(PJS.list.hasSelected(form.elements[i],en) == false){
						if(form_element_focus == null ) form_element_focus = form.elements[i];
						form_element_msg.push("The field must be selected");
						PJS.addClass(form.elements[i].parentNode,"form-element-error");
						berr = true;
					} else {
						PJS.removeClass(form.elements[i].parentNode,"form-element-error");
					}
				} else {
					if(form.elements[i].value == ""){
						if(form_element_focus == null ) form_element_focus = form.elements[i];
						form_element_msg.push("The field must be entered");
						PJS.addClass(form.elements[i],"form-element-error");
						berr = true;
					} else {
						PJS.removeClass(form.elements[i],"form-element-error");
					}
				}				
			} else {
				vfs.push(form.elements[i].name);
			}
		}
	}
	if(form_element_focus != null){
		form_element_focus.focus();
	}
	if(form_element_msg.length > 0){
		//alert(form_element_msg[0]);
	}
	if(berr == true){
		return false;
	}
	var r = validator_form(form,vfs);
	if(r == false){
		return false;
	}
	if(checkafter != ""){
		var _ca = eval(checkafter);
		if(_ca == false){
			return false;
		}
	}
	return true;
};
/////////////////////////////////////////////////////////////////////////////
//onload
addLoadEvent(function(){
	PJS.validator.registerFormAction();
});
/////////////////////////////////////////////////////////////////////////////